package com.hzhc.controller.sysmanage;

import cn.hutool.core.util.NumberUtil;
import com.alibaba.fastjson2.JSONObject;
import com.hzhc.annotation.SysTemLog;
import com.hzhc.common.BaseController;
import com.hzhc.entity.SysYzsMenu;
import com.hzhc.enumBean.ResultUtilCodeEnum;
import com.hzhc.service.SysYzsMenuService;
import com.hzhc.utils.ResultUtil;
import com.hzhc.vo.TreeVo;
import lombok.RequiredArgsConstructor;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author dongcy
 * @date 2024/4/12 9:17
 * @description SysYzsMenuController
 */
@RestController
@RequestMapping("/sysYzsMenu")
@RequiredArgsConstructor
public class SysYzsMenuController extends BaseController {

    private final SysYzsMenuService sysYzsMenuService;

    /**
     * 获取树形菜单
     * @param request
     * @param data
     * @return
     */
    @RequestMapping("/getMenuTree")
    public ResultUtil<List<TreeVo>> getMenuTree(HttpServletRequest request, @RequestBody JSONObject data) {
        Integer pid = 0;
        if (NumberUtil.isNumber(data.getString("pid"))) {
            pid = data.getInteger("pid");
        }
        Integer type =data.getInteger("type");
        List<TreeVo> list = new ArrayList<>();
        if (type != null && type==0) {//目录
            list = sysYzsMenuService.getMlMenuTree(Long.valueOf(pid));
        }else{
             list = sysYzsMenuService.getMenuTree(Long.valueOf(pid));
        }


        return ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 获取菜单列表
     * @param data
     * @return
     */
    @RequestMapping("/getMenuList")
    public ResultUtil<List<SysYzsMenu>> getMenuList(@RequestBody JSONObject data) {
        String name = data.getString("title");
        List<SysYzsMenu> list = sysYzsMenuService.getMenuListAll(name);
        return ResultUtil.build(list, ResultUtilCodeEnum.SUCCESS);
    }

    /**
     * 菜单管理增加
     * @param request
     * @param vo
     * @return
     */
    @PostMapping("/saveMenu")
    @SysTemLog(operModul = "系统管理-一站式菜单管理增加", operType = "增加", operDesc = "一站式菜单管理增加功能", modular="PC")
    public ResultUtil<T> saveMenu(HttpServletRequest request, @RequestBody SysYzsMenu vo) {
        if(vo.getParentId()==null){
            vo.setParentId(0L);
        }
        boolean bool = sysYzsMenuService.saveSysMenu(vo);
        if(bool){
            return ResultUtil.ok();
        }
        return ResultUtil.fail();
    }

    /**
     * 菜单管理修改
     * @param request
     * @param data
     * @return
     */
    @PostMapping("/updateMenu")
    @SysTemLog(operModul = "系统管理-一站式菜单管理修改", operType = "修改", operDesc = "一站式菜单管理修改功能", modular="PC")
    public ResultUtil<T> updateMenu(HttpServletRequest request, @RequestBody JSONObject data) {
        SysYzsMenu vo = data.to(SysYzsMenu.class);
        boolean bool = sysYzsMenuService.updateMenu(vo);
        if(bool){
            return ResultUtil.ok();
        }
        return ResultUtil.fail();
    }

    /**
     * 菜单管理删除
     * @param request
     * @param model
     * @param id
     * @return
     */
    @RequestMapping("/delMenu/{id}")
    @SysTemLog(operModul = "系统管理-一站式菜单管理删除", operType = "删除", operDesc = "一站式菜单管理删除功能", modular="PC")
    public ResultUtil<T> userDel(HttpServletRequest request, Model model, @PathVariable("id") Long id) {
        boolean bool = sysYzsMenuService.removeById(id);
        if(bool){
            return ResultUtil.ok();
        }
        return ResultUtil.fail();
    }

}
